﻿using System.Web.Mvc;
using CID.Domain;
using CID.Web.Models;
using CID.Web.Support.Ajax;

namespace CID.Web.Controllers
{
    public abstract class AjaxEnabledBaseCRUDController<TModelInterface, TModel, TViewModel> : BaseCRUDController<TModelInterface, TModel, TViewModel>
        where TModelInterface : IEntity
        where TModel: class, TModelInterface
        where TViewModel : BaseViewModel<TModelInterface>, IAjaxEnabledViewModel, new()
    {
        protected override void PrepareViewModelForView(TViewModel viewModel)
        {
            base.PrepareViewModelForView(viewModel);

            viewModel.PostUrl = Url.RouteUrl(Url.RequestContext.RouteData.Values);
        }

        public override ActionResult Create(TViewModel viewModel)
        {
            var result = base.Create(viewModel);
            if (Request.IsAjaxRequest()) result = Json(AjaxActionResult.Create(result, Url));
            return result;
        }

        public override ActionResult Edit(int id, TViewModel viewModel)
        {
            var result = base.Edit(id, viewModel);
            if (Request.IsAjaxRequest()) result = Json(AjaxActionResult.Create(result, Url));
            return result;
        }

    }
}